package com.hongyu.smart.village.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hongyu.smart.village.R;
import com.hongyu.smart.village.event.Event;
import com.hongyu.smart.village.model.entity.BaseResponse;
import com.hongyu.smart.village.model.response.DomicileResp;
import com.hongyu.smart.village.network.BaseObserver;
import com.hongyu.smart.village.network.RetrofitHelper;
import com.hongyu.smart.village.network.RxUtil;
import com.hongyu.smart.village.network.api.AppInfoApi;
import com.hongyu.smart.village.ui.base.BaseToolbarActivity;
import com.hongyu.smart.village.ui.widget.ToastTool;

import butterknife.BindView;
import butterknife.OnClick;

public class DomicileInforActivity extends BaseToolbarActivity {

    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.tv_address)
    TextView tvAddr;
    @BindView(R.id.et_number)
    EditText etNumber;
    @BindView(R.id.et_qty)
    EditText etQty;
    @BindView(R.id.et_qty2)
    EditText etQty2;

    private int id=0,qty=0,qty2=0,userId=0,redionId=0;
    private String code="",personName="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public String getToolBarTitle() {
        return "家庭信息";
    }

    @Override
    protected void initActivityData() {

        if (resp!=null) {
            userId = resp.getId();
            redionId = resp.getRegionId();
            tvName.setText(resp.getNickname());
            tvAddr.setText(resp.getRegionName());
//            httpGetAllAreaCode(redionId);
            httpGetDomicile();
        }else {
            ToastTool.show("暂未登录");
        }
    }


    @Override
    protected void initContentView() {




    }

    private void httpGetDomicile() {
        RetrofitHelper.create(AppInfoApi.class)
                .domicileFind(userId)
                .compose(RxUtil.observervableToMain())
                .as(RxUtil.bindLifecycle( this))
                .subscribe(new BaseObserver<DomicileResp>(getLoadingDialog()){
                    @Override
                    public void onSuccess(BaseResponse<DomicileResp> response) {
                        if (response.getData()!=null) {

                            setViewData(response.getData());








                        }
                        else {
                            httpGetAllAreaCode(redionId);
                        }
                    }
                });
    }

    private void setViewData(DomicileResp data) {
        String num=data.getNumber();
        redionId=data.getBelongId();
        id=data.getValue();
        etQty.setText(String.valueOf(data.getPersonQty()));
        tvNumber.setText(num.substring(0,num.length()-3));
        etNumber.setText(num.substring(num.length()-3));


    }

    private void httpGetAllAreaCode(Integer regionId) {
        RetrofitHelper.create(AppInfoApi.class)
                .regionParentCode(String.valueOf(regionId))
                .compose(RxUtil.observervableToMain())
                .as(RxUtil.bindLifecycle( this))
                .subscribe(new BaseObserver<String>(getLoadingDialog()){
                    @Override
                    public void onSuccess(BaseResponse<String> response) {
                        if (response.getData()!=null) {

                            tvNumber.setText(response.getData());

//





                        }
                    }
                });
    }
    @OnClick({R.id.btn_save})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.btn_save:
                saveFamilyInfor();
                break;
        }
    }

    private void saveFamilyInfor() {
        try {
            qty=Integer.valueOf(etQty.getText().toString().trim());
        }catch (Exception e){

        }
        try {
            qty2=Integer.valueOf(etQty2.getText().toString().trim());
        }catch (Exception e){

        }


        code=tvNumber.getText().toString().trim()+etNumber.getText().toString().trim();
        RetrofitHelper.create(AppInfoApi.class)
                .domicileSave(id,userId,qty,qty2,code)
                .compose(RxUtil.observervableToMain())
                .as(RxUtil.bindLifecycle( this))
                .subscribe(new BaseObserver<Object>(getLoadingDialog()){
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
                        if (response.getData()!=null) {

                            httpGetDomicile();







                        }
                    }
                });
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_family_infor;
    }

    @Override
    protected void onEventProcess(Event event) {

    }
}